package com.qf.dbUtil.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.qf.dbUtil.common.annotation.CheckLogin;
import com.qf.dbUtil.common.result.R;
import com.qf.dbUtil.common.result.ResponseEnum;
import com.qf.dbUtil.entity.User;
import com.qf.dbUtil.service.SmsService;
import com.qf.dbUtil.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能说明
 *
 * @author yihuan
 * @data 2020-10-15  10:22:05
 */
@RestController
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("/admin")
@Api(description = "后台管理-Api")
public class AdminController {
    @Autowired
    private UserService userService;

    @Autowired
    private SmsService smsService;

    Map<String,String> checkMap = new HashMap<>();
    /**
     * 登录
     * @param userName
     * @param passWord
     * @return
     */

    @PostMapping("/user/login")
    @ApiOperation("用户名密码登录")
    public R login(
            @ApiParam(value = "用户名",required = true) @RequestParam String userName,
            @ApiParam(value = "密码",required = true) @RequestParam  String passWord){
        User login = userService.login(userName, passWord);
        return new R(ResponseEnum.SUCCESS,login);
    }
    /**
     * 取钱
     * @param money
     * @param userstr json字符串
     */
    @PutMapping("/user/getmoney")
    @CheckLogin
    @ApiOperation("根据账号密码取钱")
    public R getMoney(
            @ApiParam(value = "金额",required = true)BigDecimal money,
            @ApiParam(value = "用户信息",required = true)String userstr){
        //使用fastJson将字符串转换为对象
        User user = JSONObject.parseObject(userstr,User.class);
        userService.getMoney(user,money);
        return new R(ResponseEnum.SUCCESS);
    }

    /**
     * 生成验证码
     * @param phone
     * @return
     */

    @GetMapping("/user/getCheckCode/{phone}")
    @ApiOperation("生成验证码")
    public R getCheckCode(
            @ApiParam(value = "电话号码",required = true) @PathVariable("phone") String phone) throws ClientException {
        //随机生成1000-9999的四位数   Math.random()*(max-min+1)+min;
        int i =(int)(Math.random()*(9999-1000+1)+1000);
        //发短信
        smsService.sendCheckCode(phone,i+"");
        //保存生成的验证码
        checkMap.put(phone,i+"");
        return new R(ResponseEnum.SUCCESS,null);
    }
    /**
     * 手机验证码登录
     * @param phone
     * @param checkCode
     * @return
     */

    @GetMapping("/user/loginFast")
    @ApiOperation("手机验证码登录")
    public R loginFast(
            @ApiParam(value = "电话号码",required = true) @RequestParam String phone,
            @ApiParam(value = "验证码",required = true) @RequestParam String checkCode){
        System.out.println(phone+""+checkCode);
        //查询手机号
        User user = userService.findByPhone(phone);
        //检查验证码
        String sc = checkMap.get(phone);
        if(sc.equals(checkCode)==false){
            return new R(ResponseEnum.CHECKCODE_ERROR,null);
        }
        return new R(ResponseEnum.SUCCESS,user);
    }
}
